using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class DressingRoomMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.dr_Menu, new Point(0, -107));
    private static bool inited;

    public DressingRoomMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            menu.OnMenuClose += OnMenuClose;
            inited = true;
        }
    }

    public static void InitDressingRoom()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());
        var model = new UIMenuItem(TextSetting.dr_MenuItem_Model);
        var comp = new UIMenuItem(TextSetting.dr_MenuItem_Components);
        var prop = new UIMenuItem(TextSetting.dr_MenuItem_Props);
        var tattoo = new UIMenuItem(TextSetting.dr_MenuItem_Tattoo);
        var badge = new UIMenuItem(TextSetting.dr_MenuItem_Badge);
        var adv = new UIMenuItem(TextSetting.dr_MenuItem_AdvancedSettings, TextSetting.dr_MenuItem_AdvancedSettings_dct);
        var save = new UIMenuItem(TextSetting.dr_MenuItem_SaveSkin);
        var load = new UIMenuItem(TextSetting.dr_MenuItem_LoadSkin);

        menu.AddItem(model);
        menu.AddItem(comp);
        menu.AddItem(prop);
        menu.AddItem(tattoo);
        menu.AddItem(badge);
        menu.AddItem(adv);
        menu.AddItem(save);
        menu.AddItem(load);

        menu.BindMenuToItem(ModelMenu.menu, model);
        menu.BindMenuToItem(ComponentMenu.menu, comp);
        menu.BindMenuToItem(PropMenu.menu, prop);
        menu.BindMenuToItem(TattooMenu.menu, tattoo);
        menu.BindMenuToItem(BadgeMenu.menu, badge);

        menu.OnItemSelect += (sender, item, index) =>
        {
            switch (index)
            {
                case 0:
                    ModelMenu.Open();
                    break;
                case 1:
                    ComponentMenu.Open();
                    break;
                case 2:
                    PropMenu.Open();
                    break;
                case 3:
                    TattooMenu.Open();
                    break;
                case 4:
                    BadgeMenu.InitBadgeMenu();
                    break;
                case 5:
                    DressingRoomAdvancedMenu.InitAdvancedMenu();
                    break;
                case 6:
                    RecordMenu.InitRecordMenu(true);
                    break;
                case 7:
                    RecordMenu.InitRecordMenu(false);
                    break;
            }
            Main.OnMainMenuHidden();
            menu.Visible = false;
        };

        menu.RefreshIndex();
        menu.Visible = true;
    }

    private static void OnMenuClose(UIMenu sender)
    {
        // 仅隐藏菜单，避免在用户退格时大量状态切换引发卡死；实际结束由显式逻辑触发
        try { LogHelper.WriteInfo("DressingRoomMenu closed"); } catch { }
        menu.Visible = false;
    }
}


